<?php

namespace AdminModule;

class FilesPresenter extends \BasePresenter
{

	public function renderDefault()
	{
		$filesModel = $this->context->models->files;
		$this->template->projectFiles = $filesModel->getProjectFiles()->order('project_id');
		$this->template->taskFiles = $filesModel->getTaskFiles()->order('task_id');
	}

	public function renderEditProjectFile($id)
	{
		$filesModel = $this->context->models->files;
		$file = $filesModel->getProjectFile(array('id' => $id));
		$file['type'] = 'project';
		$this['editFile']->setValues($file);

		$this->template->filename = $file['name'];
		$this->template->filepath = $file['path'];
	}
	
	/** @secured */
	public function handleDeleteProjectFile($id)
	{
		$filesModel = $this->context->models->files;
		if($filesModel->deleteProjectFile($id)){
			$this->flashMessage('Soubor byl úspěšně smazán');
			$this->redirect('this');
		}else{
			$this->flashMessage('Chyba při mazání souboru.','error');
		}
	}
	
	public function renderEditTaskFile($id)
	{
		$filesModel = $this->context->models->files;
		$file = $filesModel->getTaskFile(array('id' => $id));
		$file['type'] = 'task';
		$this['editFile']->setValues($file);

		$this->template->filename = $file['name'];
		$this->template->filepath = $file['path'];
	}

	/** @secured */
	public function handleDeleteTaskFile($id)
	{
		$filesModel = $this->context->models->files;
		if($filesModel->deleteTaskFile($id)){
			$this->flashMessage('Soubor byl úspěšně smazán');
			$this->redirect('this');
		}else{
			$this->flashMessage('Chyba při mazání souboru.','error');
		}
	}
	
	public function renderTypes()
	{
		$filesModel = $this->context->models->files;
		$this->template->filetypes = $filesModel->getFileTypes();
	}

	public function renderAddType()
	{
		
	}

	public function renderEditType($id)
	{
		$filesModel = $this->context->models->files;
		$fileType = $filesModel->getFileType(array('id' => $id));
		$this['editFileTypeForm']->setValues($fileType);
	}

	/** @secured */
	public function handleDeleteType($id)
	{
		$filesModel = $this->context->models->files;
		if ($filesModel->deleteFileType($id))
		{
			$this->flashMessage('Typ souboru byl úspěšně smazán');
		} else
		{
			$this->flashMessage('Chyba! Typ nelze smazat, pokud existují soubory tohoto typu', 'error');
		}
		$this->redirect('this');
	}

	public function createComponentEditFile($name)
	{
		$form = new \Nette\Application\UI\Form($this, $name);
		$form->addText('name', 'Název:');
		$form->addTextArea('description', 'Popis:');
		$form->addHidden('type');
		$form->addHidden('id');
		$form->addSubmit('save', 'Uložit');
		$form->onSuccess[] = callback($this, 'editFile');
		return $form;
	}

	public function editFile($form)
	{
		$values = $form->getValues();
		$editMethod = 'edit' . ucfirst($values['type']) . 'File';
		unset($values['type']);
		$filesModel = $this->context->models->files;
		if ($filesModel->{$editMethod}($values) !== false)
		{
			$this->flashMessage('Soubor byl úspěšně upraven');
			$this->redirect('default');
		}else{
			$this->flashMessage('Chyba při úpravě souboru','error');
		}
	}

	public function createComponentAddFileTypeForm($name)
	{
		$form = new \Nette\Application\UI\Form($this, $name);
		$form->addText('name', 'NĂˇzev:')->setRequired('Vyplňte prosím název typu');
		$form->addSubmit('send', 'PĹ™idat');
		$form->onSuccess[] = callback($this, 'addFileType');
		return $form;
	}

	public function createComponentEditFileTypeForm($name)
	{
		$form = new \Nette\Application\UI\Form($this, $name);
		$form->addText('name', 'NĂˇzev:')->setRequired('Vyplňte prosím název typu');
		$form->addHidden('id');
		$form->addSubmit('send', 'UloĹľit');
		$form->onSuccess[] = callback($this, 'editFileType');
		return $form;
	}

	public function addFileType($form)
	{
		$values = $form->getValues();
		$filesModel = $this->context->models->files;
		if ($filesModel->addFileType($values['name']))
		{
			$this->flashMessage('Typ souboru byl úspěšně přidán');
			$this->redirect('types');
		} else
		{
			$this->flashMessage('Chyba! Tento typ souboru již existuje', 'error');
		}
	}

	public function editFileType($form)
	{
		$values = $form->getValues();
		$filesModel = $this->context->models->files;
		if ($filesModel->editFileType($values) !== false)
		{
			$this->flashMessage('Typ souboru byl úspěšně upraven');
			$this->redirect('types');
		} else
		{
			$this->flashMessage('Chyba při úpravě typu!', 'error');
		}
	}

}